#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-03-16 16:26:28

import pymysql
import os
try:
    import ConfigParser
except ModuleNotFoundError:
    import configparser as ConfigParser


def initDB():
    config_file = os.path.join(os.path.dirname(
        __file__), "..", "conf", "server.ini")
    # print(os.path.dirname(__file__))
    cfg = ConfigParser.ConfigParser()
    cfg.read(config_file)
    db_info = dict(
        username=cfg.get('db', 'user'),
        password=cfg.get('db', 'password'),
        host=cfg.get('db', 'host'),
        port=cfg.get('db', 'port')
    )
    return db_info


def selectRow(sql, db):
    db_info = initDB()
    try:
        conn = pymysql.connect(host=db_info['host'],
                               port=int(db_info['port']),
                               user=db_info['username'],
                               passwd=db_info['password'],
                               db=db)
        cursor = conn.cursor()
        cursor.execute(sql)
        effect_rows = cursor.fetchall()
        return effect_rows
    except pymysql.OperationalError:
        return '404'
